﻿$(function () {
    warehouseFan();
});

function warehouseFan() {
    layui.use('layer', function () {
        var form = layui.form;

        var warehouseID = localStorage["warehouseID"]
        // 创建表格实例
        $.ajax({
            url: "/Warehouses/LedgerInfowarehouseFan",
            type: "get",
            data: {
                warehouseID: warehouseID,
            },
            success: function (data) {
                data = JSON.parse(data);
                console.log(data.data);
                // 在 ajax 请求成功后，使用正确的表格 ID 进行 reload
                $(data.data).each(function () {
                    $("#warehouseNumber").val(this.warehouseNumber)
                    $("#warehouseName").val(this.warehouseName)
                    $("#warehouseArea").val(this.warehouseArea)
                    $("#selectWarehouseArea").val(this.selectWarehouseArea)
                    $("#positionNumber").val(this.positionNumber)
                    $("#principal").val(this.principal)
                    var lswarehouseState = this.warehouseState
                    $("[name='warehouseState']").each(function () {
                        if (lswarehouseState.toString() == $(this).val()) {
                            $(this).prop("checked", true);
                        }
                    })
                    $("#warehouseRemark").val(this.warehouseRemark)
                    form.render();
                })
            }
        });
    })
}

layui.use('layer', function () {
    var $ = layui.$;
    var layer = layui.layer;
    var util = layui.util;
    var form = layui.form;
    var table = layui.table;
    // 对弹层中的表单进行初始化渲染
    form.render();
    // 表单提交事件
    form.on('submit(demo-login)', function (data) {
        var field = data.field;
        var obj1 = {
            warehouseID: localStorage["warehouseID"],
            warehouseNumber: field.warehouseNumber,
            warehouseName: field.warehouseName,
            warehouseArea: field.warehouseArea,
            selectWarehouseArea: field.selectWarehouseArea,
            positionNumber: field.positionNumber,
            principal: field.principal,
            warehouseState: field.warehouseState,
            warehouseRemark: field.warehouseRemark
        };
        console.log(field)
        console.log(obj1)
        $.ajax({
            url: '/Warehouses/LedgerInfowarehouseUpdate',
            type: 'post',
            data: { obj1, token: localStorage.getItem("token") },
            success: function (res) {
                var s = JSON.parse(res)
                console.log(res)
                if (s.code == 1) {
                    layer.alert("修改成功");
                    //setTimeout(function () {
                    //    location.reload();
                    //}, 2000);
                    location.href = "/Warehouses/warehouseShow";
                }
                else {
                    layer.alert("修改成功");
                }
            }
        })
        return false; // 阻止默认 form 跳转
    });

});


